package com.fanchuan.uzx.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.fanchuan.uzx.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DrunbPullToRefreshView extends PullToRefreshListView {

    private Context mContext;

	public DrunbPullToRefreshView(Context context,
			com.handmark.pulltorefresh.library.PullToRefreshBase.Mode mode) {
		super(context, mode);
        mContext = context;

        init();
	}

	public DrunbPullToRefreshView(Context context, AttributeSet attrs) {
		super(context, attrs);
        mContext = context;
        init();
	}

	public DrunbPullToRefreshView(Context context) {
		super(context);
        mContext = context;
        init();
	}

    private SimpleDateFormat mDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm");
    /**
     * Set last update by system.currentmillis
     * @param millis
     */
    public void setLastUpdate(long millis) {
    	Log.d("DrunbListView", "setLastUpdate: " + millis);
        if (millis < 1) {
            return;
        }
        String date = mDateFormat.format(new Date(millis));
        setLastUpdatedLabel(String.format(
                getString(R.string.nf_last_update_template), date));
    }

    Drawable getDrawable(int resId) {
        return mContext.getResources().getDrawable(resId);
    }

    String getString(int resId) {
        return mContext.getResources().getString(resId);
    }

    public void init() {
        setMode(Mode.PULL_DOWN_TO_REFRESH);

        setLoadingDrawable(getDrawable(R.drawable.ic_refreshing), Mode.PULL_DOWN_TO_REFRESH);
        setPullLabel(getString(R.string.nf_pull_label));
        setRefreshingLabel(getString(R.string.nf_refreshing_label));
        setReleaseLabel(getString(R.string.nf_release_label));

        setShowIndicator(false);
//        getRefreshableView().setSelector(R.drawable.list_selector);
        getRefreshableView().setCacheColorHint(Color.parseColor("#00000000"));
//        getRefreshableView().setDividerHeight(0);
    }
    private boolean mFooterAdded = false;
    private View mFootView = null;
    
    public synchronized void addFootView(int layoutId) {
    	if (mFooterAdded) {
    		return;
    	}
    	mFootView = LayoutInflater.from(mContext).inflate(layoutId, null); 
    	getRefreshableView().addFooterView(
    			mFootView
				);
    	mFooterAdded = true;
    }
    
    public synchronized void removeFootView() {
    	if (!mFooterAdded) {
    		return;
    	}
    	getRefreshableView().removeFooterView(mFootView);
    	mFootView = null;
    	mFooterAdded = false;
    }
}
